﻿using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using TestCluster.usermodels;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Uow;

namespace TestCluster.bindmodels
{
    /// <summary>
    /// 用户绑定部门 一个人可以属于多个部门
    ///</summary>
    [TypeFilter(typeof(RoleAttribute), Arguments = new object[] { "data", "add" })]
    public class BindUserPartAppService : TestClusterAppService
    {
        /// <summary>
        /// 
        /// </summary>
        // private readonly IRepository<BindUserPart, int> _repository;

        private readonly ITestClusterDbContext _dbContext;


        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbContext"></param>
        public BindUserPartAppService(ITestClusterDbContext dbContext)
        {
            // _repository = repository;
            _dbContext = dbContext;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<PagedResultDto<BindUserPartListDto>> GetListAsync(int page = 1, int size = 20)
        {

            var query = _dbContext.BindUserPart.Where(t => 1 == 1)
                .OrderByDescending(xy => xy.Id);

            PagedResultDto<BindUserPartListDto> pagedResultDto = new PagedResultDto<BindUserPartListDto>();
            pagedResultDto.TotalCount = await query.CountAsync();
            var userList = await query.Page(page, size).ToListAsync();
            var temList = ObjectMapper.Map<List<BindUserPart>, List<BindUserPartListDto>>(userList);
            pagedResultDto.Items = temList;
            return pagedResultDto;
        }

        /// <summary>
        /// 根据ID获取单项用户绑定部门 一个人可以属于多个部门
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public BindUserPartDto GetByIdAsync(int id)
        {
            var query = _dbContext.BindUserPart.Where(t => t.Id == id).FirstOrDefault();
            var temList = ObjectMapper.Map<BindUserPart, BindUserPartDto>(query);
            return temList;
        }

        /// <summary>
        /// 根据ID获取待更新单项信息用户绑定部门 一个人可以属于多个部门
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public BindUserPartUpdateDto GetInfoForUpdateAsync(int id)
        {
            var query = _dbContext.BindUserPart.Where(t => t.Id == id)
                .FirstOrDefault();
            var temList = ObjectMapper.Map<BindUserPart, BindUserPartUpdateDto>(query);
            return temList;
        }


        /// <summary>
        /// 添加一个用户绑定部门 一个人可以属于多个部门
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [UnitOfWork(false)]
        [HttpPost]
        public async Task<int> CreateItemAsync(BindUserPartAddDto input)
        {

            var newu = ObjectMapper.Map<BindUserPartAddDto, BindUserPart>(input);
            _dbContext.Add(newu);
            await _dbContext.SaveChangesAsync();
            //var backinfo = ObjectMapper.Map<BindUserPart, BindUserPartDto>(updated);
            return newu.Id;
        }
        /// <summary>
        /// 更新一个用户绑定部门 一个人可以属于多个部门
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [UnitOfWork(false)]
        [HttpPost]
        public async Task<int> UpdateItemAsync(BindUserPartUpdateDto input)
        {
            var info = await _dbContext.BindUserPart.Where(x => x.Id == input.Id).FirstOrDefaultAsync();
            if (info == null || info == default)
            {
                throw new TestClusterException("需要查询的信息不存在", "404");
            }
            ObjectMapper.Map<BindUserPartUpdateDto, BindUserPart>(input, info);
            //var updated = await _repository.UpdateAsync(newu);
            //var backinfo = ObjectMapper.Map<BindUserPart, BindUserPartDto>(info);
            return input.Id;
        }



    }
}
